import 'dart:io';

import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:path_provider/path_provider.dart';

class FileUtil {
  /// 获取APP下文件路径
  /// Android:  getFilesDir
  /// iOS:  NSApplicationSupportDirectory
  static Future<String> getFilesDir() async {
    Directory filesDir = await getApplicationSupportDirectory();
    return filesDir.path;
  }

  /// 获取APP下缓存路径
  /// Android:  NSCachesDirectory
  /// iOS:  getCacheDir
  static Future<String> getCacheDir() async {
    Directory cacheDir = await getTemporaryDirectory();
    return cacheDir.path;
  }

  /// 保存图片和视频到相册中
  /// Android：
  ///     在 AndroidManifest.xml 文件中添加
  ///     <application android:requestLegacyExternalStorage="true" .....>
  ///     获取存储权限storage
  ///
  /// iOS：
  ///     在 Info.plist 文件中添加
  ///     <key>NSPhotoLibraryAddUsageDescription</key>
  ///     <string>请求添加图片到相册</string>
  ///     获取写入相册权限photosAddOnly
  ///
  /// 权限申请详情，请查看：permission_util.dart 文件
  static saveImgOrVideoToGallery(filePath,
      {bool isDelSourceFile = false}) async {
    await ImageGallerySaver.saveFile(filePath);
    if (isDelSourceFile) {
      File(filePath).delete();
    }
  }

  /// 获取文件名字，包括后缀
  static String getFileName(String filePath) {
    return filePath.substring(filePath.lastIndexOf('/') + 1, filePath.length);
  }

  /// 获取文件名后缀
  static String getFileSuffix(String filePath) {
    return filePath.substring(filePath.lastIndexOf('.') + 1, filePath.length);
  }
}
